package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketGoodsVo;
import com.cskaoyan.market.db.domain.MarketOrderVo;
import com.cskaoyan.market.db.domain.MarketRow;
import com.cskaoyan.market.db.mapper.GoodsMapper;
import com.cskaoyan.market.db.mapper.MarketRowMapper;
import com.cskaoyan.market.db.mapper.OrderMapper;
import com.cskaoyan.market.service.AdminStatService;
import com.cskaoyan.market.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminStatServiceImpl implements AdminStatService {
    @Override
    public List<MarketOrderVo> order() {
        SqlSession session = MybatisUtil.getSession();
        OrderMapper mapper = session.getMapper(OrderMapper.class);
        List<MarketOrderVo> marketOrderVos = mapper.selectOrderByTime();
        session.commit();
        session.close();
        return marketOrderVos;
    }

    @Override
    public List<MarketGoodsVo> goods() {
        SqlSession session = MybatisUtil.getSession();
        GoodsMapper mapper = session.getMapper(GoodsMapper.class);
        List<MarketGoodsVo> marketGoodsVos = mapper.selectOrderByTime();
        session.commit();
        session.close();
        return marketGoodsVos;
    }

    @Override
    public Map<String, List<Object>> user() {
        Map<String,List<Object>> map=new HashMap<>();

        // columns
        List<Object> columns=new ArrayList<>();
        columns.add("day");
        columns.add("users");
        map.put("columns",columns);

        // rows
        SqlSession session = MybatisUtil.getSession();
        MarketRowMapper mapper = session.getMapper(MarketRowMapper.class);
        List<MarketRow> marketRows = mapper.selectGroupBy();
        List<Object> rows=new ArrayList<>();
        rows.addAll(marketRows);
        map.put("rows",rows);
        session.commit();
        session.close();

        return map;
    }
}
